﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "infoconfirm.h"
#include "login.h"
#include "mydatabase.h"
#include <QString>
#include <qmessagebox.h>
#include <QVariantList>
#include <QDate>
#include <QDateTime>
#include <QDateEdit>
#include <QPixmap>
#include <QTableWidget>
#include <QApplication>
#include <QFileDialog>


MainWindow::MainWindow(QString account,QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->stackedWidget->setCurrentIndex(0);
//    ui->roomManageBtn->setFixedSize(120, 60);
    ui->employerManageBtn->setFixedSize(200, 60);
    ui->roomEditBtn->setFixedSize(200, 60);
    ui->inRoomBtn->setFixedSize(200, 60);
    ui->outRoomBtn->setFixedSize(200, 60);
    ui->roomInfoBtn->setFixedSize(200, 60);
    ui->hotelBill_pushButton_rzx->setFixedSize(200, 60);
    ui->pushButtonExit->setFixedSize(200,60);
//    ui->addRoomWindowBtn->setFixedSize(800, 50);
//    ui->changeRoomPriceWindowBtn->setFixedSize(800, 50);
//    ui->removeRoomWindowBtn->setFixedSize(800, 50);
//    ui->addRoomTypeWIndowBtn->setFixedSize(800, 50);

    ui->comboBoxLv->addItem("30");
    ui->comboBoxLv->addItem("40");
    ui->comboBoxWorkerLv->addItem("30");
    ui->comboBoxWorkerLv->addItem("40");
    ui->outRoomBtn_dhy->setShortcut(Qt::Key_Enter);
    ui->pushButton_6->setShortcut(Qt::Key_Enter);
    ui->pushButton_5->setShortcut(Qt::Key_Enter);
    ui->pushButton_7->setShortcut(Qt::Key_Enter);
    //判断登录的是不是特殊账户
    if(account == "admin")
    {
        user.append(QVariant("行号：0"));
        user.append(QVariant("admin"));
        user.append(QVariant("123456"));
        user.append(QVariant("超级管理员"));
        user.append(QVariant("410000000000000000"));
        user.append(QVariant(30));
    }

    //系统初始化
    system_Init(account);

    QString userName = user.value(3).toString();

    //提示登录的是谁
    QMessageBox::information(this, "admin", userName + "你好");

    //根据等级按钮是否启用
    if(user.value(5).toInt() < 31)
    {
        ui->employerManageBtn->setDisabled(false);
        ui->roomEditBtn->setDisabled(false);
        ui->inRoomBtn->setDisabled(false);
        ui->outRoomBtn->setDisabled(false);
        ui->roomInfoBtn->setDisabled(false);
    }
    else
    {
        ui->roomInfoBtn->setDisabled(false);
        ui->outRoomBtn->setDisabled(false);
        ui->inRoomBtn->setDisabled(false);
        ui->roomEditBtn->setDisabled(true);
        ui->employerManageBtn->setDisabled(true);
    }

    //窗口下方账号、等级显示更新
    ui->zhanghao_rzx->setText( user.value(3).toString());
    ui->lv_rzx->setText(user.value(5).toString());

}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_employerManageBtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
}

void MainWindow::on_inRoomBtn_clicked()
{
    //设置两个时间日期
    ui->startDateEdit->setDate(QDate::currentDate());
    ui->endDateEdit->setDate(QDate::currentDate().addDays(1));

    //切换stackedWidget页面
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_outRoomBtn_clicked()
{

    ui->stackedWidget->setCurrentIndex(1);
}

//客房信息
void MainWindow::on_roomInfoBtn_clicked()
{
    //获取数据库信息
    QVariantList find = back_roomClass->Find("class != ' '");
    QString roomClass[find.length() / 6];   //房间类型
    QString image[find.length() / 6];   //图片
    QString brief[find.length() / 6];   //房间简介
    QString numbers[find.length() / 6]; //房间号
    QString prive[find.length() / 6];   //房间价格
    for (int i = 0;i < find.length() / 6;i++) {
        roomClass[i] = find.value(i*6 + 1).toString();
        image[i] = find.value(i*6 + 2).toString();
        brief[i] = find.value(i*6 + 3).toString();
        numbers[i] = find.value(i*6 + 4).toString();
        prive[i] = find.value(i*6 + 5).toString();
    }

    //设置tableWidget的单元格多少
    ui->tableWidget_rzx->setRowCount((find.length() / 6 % 2 + find.length() / 6 / 2) * 6);
    ui->tableWidget_rzx->setColumnCount(5);

    for (int i = 0; i < find.length() / 6;i++)
    {
        //设置图片
        QLabel *imageQlabel = new QLabel();
        imageQlabel->setPixmap(QPixmap(image[i]));
        imageQlabel->setScaledContents(true);
        //设置各种类
        QLabel *roomClassQlabel = new QLabel(roomClass[i]);
        QLabel *roomPriceTextQlabel = new QLabel("房间价格:");
        QLabel *roomPriceQlabel = new QLabel(prive[i]);
        QLabel *roomResidueTextQlabel = new QLabel("房间号:");
        QLabel *roomResidueQlabel = new QLabel(numbers[i]);
        QLabel *roomIntroTextQlabel = new QLabel("房间简介:");
        QLabel *roomIntroQlabel = new QLabel(brief[i]);

        if(i%2 == 0)    //两列中的第一列
        {
            //设置图片
            ui->tableWidget_rzx->setSpan(i / 2 * 6, 0, 1, 2);   //合并
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6, 1,imageQlabel); //设置图片
            ui->tableWidget_rzx->setRowHeight(i / 2 * 6,250);

            //设置房间类型
            ui->tableWidget_rzx->setSpan(i / 2 * 6 + 1 , 0, 1, 2);   //合并
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 1, 0,roomClassQlabel);

            //房间价格：
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 2, 0,roomPriceTextQlabel);
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 2, 1,roomPriceQlabel);

            //房间号：
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 3, 0,roomResidueTextQlabel);
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 3, 1,roomResidueQlabel);

            //房间简介：
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 4, 0,roomIntroTextQlabel);
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 4, 1,roomIntroQlabel);

        }
        else
        {
            //设置图片
            ui->tableWidget_rzx->setSpan(i / 2 * 6, 3,1, 2);   //合并
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6, 3,imageQlabel); //设置图片
            ui->tableWidget_rzx->setRowHeight(i / 2 * 6,250);

            //设置房间类型
            ui->tableWidget_rzx->setSpan(i / 2 * 6 + 1,3, 1, 2);   //合并
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 1, 4,roomClassQlabel);

            //房间价格：
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 2, 3,roomPriceTextQlabel);
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 2, 4,roomPriceQlabel);

            //房间号：
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 3, 3,roomResidueTextQlabel);
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 3, 4,roomResidueQlabel);

            //房间简介：
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 4, 3,roomIntroTextQlabel);
            ui->tableWidget_rzx->setCellWidget(i / 2 * 6 + 4, 4,roomIntroQlabel);


        }

    }

    //隐藏网格线
    ui->tableWidget_rzx->setShowGrid(false);
    //设置两列的宽度
    ui->tableWidget_rzx->setColumnWidth(2,50);
    //设置"房间号:、房间价格:、房间简介:"列的宽度
    ui->tableWidget_rzx->setColumnWidth(0,100);
    ui->tableWidget_rzx->setColumnWidth(3,100);
    //设置图片的宽度
    ui->tableWidget_rzx->setColumnWidth(1,260);
    ui->tableWidget_rzx->setColumnWidth(4,260);

    //跳转页面
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_roomEditBtn_clicked()
{

    // 房间类型下拉框初始化
    addRoomTypeComboBoxInit_dhy();

    ui->stackedWidget->setCurrentIndex(3);
    ui->stackedWidgetRoomEdit->setCurrentIndex(0);
}




void MainWindow::on_addRoomWindowBtn_clicked()
{
    ui->stackedWidgetRoomEdit->setCurrentIndex(1);
}

void MainWindow::on_removeRoomWindowBtn_clicked()
{

    ui->stackedWidgetRoomEdit->setCurrentIndex(2);
}

void MainWindow::on_changeRoomPriceWindowBtn_clicked()
{

    ui->stackedWidgetRoomEdit->setCurrentIndex(3);
}

void MainWindow::on_addRoomTypeWIndowBtn_clicked()
{
    ui->stackedWidgetRoomEdit->setCurrentIndex(4);

}



void MainWindow::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(6);
}

void MainWindow::on_pushButton_4_clicked()
{
      ui->stackedWidget->setCurrentIndex(7);
      //创建员工数据表变量列表 并添加数据到列表当中
    QStringList workerList;
ui->comboBox_3->clear();
     for (int i=3;i<back_workerDataBase->Find("name!=' '").length();i+=6) {
      QString  item=back_workerDataBase->Find("name!=' '").value(i).toString();
        workerList.append(item);

     }
      ui->comboBox_3->addItems(workerList);


}

void MainWindow::on_pushButton_3_clicked()
{
     ui->stackedWidget->setCurrentIndex(8);

     QStringList workerList;

     ui->comboBox_2->clear();
      for (int i=3;i<back_workerDataBase->Find("name!=' '").length();i+=6) {

       QString  item=back_workerDataBase->Find("name!=' '").value(i).toString();

         workerList.append(item);

      }

       ui->comboBox_2->addItems(workerList);


}
//添加员工按钮
void MainWindow::on_pushButton_5_clicked()
{
    //获取输入框中的值
   QString addWorkerLVText= ui->comboBoxWorkerLv->currentText();
   QString addWorkerNameText= ui->lineEdit_4->text();
   QString addWorkerIDcardext= ui->lineEdit_5->text();
   QString addWorkerTelText= ui->lineEdit_6->text();
   QString addWorkerPwdText= ui->lineEdit_7->text();
   bool statue=false;

//调用add函数
   //数据校验
     //判断数据是否合法

   if(addWorkerTelText.length()==11&&addWorkerIDcardext.length()==18){

       QString a = QString("'%1','%2','%3','%4',%5").arg
               (addWorkerTelText,addWorkerPwdText,addWorkerNameText,addWorkerIDcardext,addWorkerLVText);
       back_workerDataBase->Add(a);
       statue=true;
   }else{
        QMessageBox::critical(this, "添加员工", "用户信息输入不合法，请重新输入");
   }

    infoConfirm *info=new infoConfirm;
   info->alertInfo(statue);
//输入结束以后清空数据

   ui->lineEdit_4->setText(" ");
   ui->lineEdit_5->setText(" ");
    ui->lineEdit_6->setText(" ");
    ui->lineEdit_7->setText(" ");
}

void MainWindow::on_pushButton_6_clicked()
{
    QString delWorker=ui->comboBox_3->currentText();
//qDebug()<<ui->comboBox_3->currentText();
QString a=QString("name = '%1'").arg(delWorker);
   bool statue= back_workerDataBase->Delete(a);
   infoConfirm *info=new infoConfirm;
  info->alertInfo(statue);

}
//提交修改的员工信息
void MainWindow::on_pushButton_7_clicked()
{
    bool statue=false;
    QString adminWorkerLVText= ui->comboBoxLv->currentText();//获取等级
    qDebug()<<adminWorkerLVText.toInt();
    QString adminWorkerIDcardext= ui->lineEdit_10->text();//获取身份证
    QString adminWorkerTelText= ui->lineEdit_11->text();//获取手机号
    QString adminWorkerPwdText= ui->lineEdit_9->text();//获取密码
    QString adminWorker=ui->comboBox_2->currentText();//获取当前下拉框中的属性值


    //数据校验
    if(adminWorkerIDcardext.length()==18){
        QString name=QString("name = '%1'").arg(adminWorker);//以name作为查找索引

        QString password=QString("password = '%1'").arg(adminWorkerPwdText);
        QString idCard=QString("adminID = '%1'").arg(adminWorkerIDcardext);
        QString phoneNumber=QString("phoneNumber = '%1'").arg(adminWorkerTelText);

        QString Lv=QString("lv = '%1'").arg(adminWorkerLVText);
    //     qDebug()<<idCard;
    //      qDebug()<<name;
          //数据提交
        back_workerDataBase->Update(name,password);
        back_workerDataBase->Update(name,idCard);
        back_workerDataBase->Update(name,phoneNumber);
        back_workerDataBase->Update(name,Lv);
        statue=true;
    }else{
        statue=false;
    }

    infoConfirm *info=new infoConfirm;
    info->alertInfo(statue);

}

void MainWindow::on_pushButton_clicked()
{
//    infoConfirm *info=new infoConfirm;
//   info->show();
}

//订房功能
void MainWindow::on_inRoomSubmit_clicked()
{
    //获取所有数据
    QString telNumber = ui->inRoomTel->text();  //手机号
    QString ID = ui->inRoomIdentity->text();    //身份证号
    QString startDate = ui->startDateEdit->text();  //起始时间
    QString endDate = ui->endDateEdit->text();  //截止时间
    QString name = ui->inRoomName->text();  //姓名
    QString number = ui->inRoomId->text();  //房间号

    //检验数据是否填写正确
    QDateTime start1 = QDateTime::fromString(startDate,"yyyy-MM-dd");
    QDateTime end1 = QDateTime::fromString(endDate,"yyyy-MM-dd");   //用户设置的时间
    if(!(telNumber.length() == 11 && isNumber(telNumber) && ID.length() == 18 && isNumber(ID) && name != "" && isChineseCharacters(name) && number != "" && start1 < end1
         && isNumber(ui->shifu_lineEdit_rzx->text())))
    {
        QMessageBox::critical(this,"Error","数据填写有误，请重新填写!");
        return ;
    }

    //获取房间号对应的房间类型
    QString find = QString("numbers like '%%1%' ").arg(number); //用以查找房间号对应的房间类型
    QVariantList findData = back_roomClass->Find(find); //存储查询到的房间类数据
    bool findClass_bit = findData.value(0) == "行号：0";
    QString roomClass;  //房间类型名称
    if (findClass_bit)   //如果有匹配的房间
        roomClass = findData.value(1).toString();
    else    //没有对应的房间的话
    {
        QMessageBox::critical(this,"Error","数据库没有对应的房间号!");
        return ;
    }

    //查看该房间是否被居住
    find = QString("number = '%1' and (startDate < '%2' and endDate > '%3')").arg(number,endDate,startDate);    //有BUG：要存入的时间包含了已经存在的时间，但还能存入
    findData = back_roomInfo->Find(find);
    bool find_bit = findData.value(0) == "行号：0"; //如果该房间当天已经被用了，那么find_bit = true
    if(find_bit)    //如果房间已经被使用了
    {
        QMessageBox::critical(this,"Error","该房间当天已被使用!");
        return ;
    }
    else
    {
        find = QString("number = '%1' and (startDate > '%2' and endDate < '%3')").arg(number,startDate,endDate);    //判断存入时间包含已经存在的时间
        findData = back_roomInfo->Find(find);
        bool find_bit = findData.value(0) == "行号：0"; //如果确实有被包含，那么find_bit = true
        if(find_bit)
        {
            QMessageBox::critical(this,"Error","该房间当天已被使用!");
            return ;
        }
    }


    //组装QString
    QString message_roomInfo = QString("'%1','%2','%3','%4','%5','%6','%7','%8'").arg(roomClass,number,startDate,endDate,name,ID,telNumber,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

    //存储实付
    int shifu_price = ui->shifu_lineEdit_rzx->text().toInt();
    qDebug()<<shifu_price;
    //应付
    findData = back_roomClass->Find( QString("numbers like '%%1%'").arg(ui->outRoomId_dhy->text()));
    qlonglong yingfu_pricce = findData.value(5).toLongLong();
    //组装QString
    QString message_hotelBill = QString("'%1','%2','%3',%4,%5,'%6'").arg
            (QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),roomClass,number,QString::number(shifu_price),QString::number(yingfu_pricce),user.value(3).toString());
    back_hotelBill->Add(message_hotelBill);

    //往数据库里存储数据
    bool add_bit = back_roomInfo->Add(message_roomInfo);
    //清空所有数据
    if(add_bit){
        ui->inRoomTel->clear();  //手机号
        ui->inRoomIdentity->clear();    //身份证号
        ui->inRoomName->clear();  //姓名
        ui->inRoomId->clear();  //房间号
        //设置两个时间日期
        ui->startDateEdit->setDate(QDate::currentDate());
        ui->endDateEdit->setDate(QDate::currentDate().addDays(1));
        //清空图片
        QString image = ":/new/images/images/imageMiss.png";
        ui->label_su->setPixmap(QPixmap(image));    //设置图片
        ui->label_su->setScaledContents(true); // 图片自适应label大小
        ui->label_su->show();
        ui->shifu_lineEdit_rzx->clear();
        QMessageBox::information(this, "message", "订房成功");
    }
    else{
        QMessageBox::information(this, "message", "订房失败");
    }

}

void MainWindow::on_escBtn_addRoom_clicked()
{
    ui->stackedWidgetRoomEdit->setCurrentIndex(0);
}


void MainWindow::on_escBtn_removeRoom_clicked()
{
 ui->stackedWidgetRoomEdit->setCurrentIndex(0);
}


void MainWindow::on_pushButton_8_clicked()
{
   ui->stackedWidgetRoomEdit->setCurrentIndex(0);
}



void MainWindow::on_pushButton_10_clicked()
{
    ui->stackedWidgetRoomEdit->setCurrentIndex(0);
}
void MainWindow::system_Init(QString account)
{
    back_workerDataBase = new MyDatabase("back","workerDataBase"); //员工数据库
    back_workerDataBase->InitDatabase("create table workerDataBase(phoneNumber char(11) PRIMARY KEY , password char(20) not null , name char(10) not null , adminID char(18) not null , lv int not null)");
    back_roomClass = new MyDatabase("back","roomClass");  //房间类型
    back_roomClass->InitDatabase("create table roomClass(class char(10) PRIMARY KEY , image char(500), brief char(300) , numbers char(300) not null , price int not null)");
    back_roomInfo = new MyDatabase("back","roomInfo");   //订房情况
    back_roomInfo->InitDatabase("create table roomInfo(class char(10) not null , number char(10) not null , startDate date not null , endDate date not null , name char(10) not null , ID char(18) not null , phoneNumber char(11) not null , checkInDate daretime not null)");
    back_hotelBill = new MyDatabase("back","hotelBill");    //酒店经营情况
    back_hotelBill->InitDatabase("create table hotelBill(date datetime not null , class char(10) not null , number char(10) not null , collectMoney int not null , collectedMoney int not null , name char(10) not null)");

    //初始化登录账户的信息
    if(account != "admin")
    user = back_workerDataBase->Find(QString("phoneNumber = '%1'").arg(account));
}
//判断是否为纯数字
bool MainWindow::isNumber(const QString &str) {
    QRegExp rx("[0-9]+");

    return rx.exactMatch(str);
}

//判断传入的数据是否为纯汉字
bool MainWindow::isChineseCharacters(const QString &str) {
    for (QChar ch : str) {
        if (ch.unicode() < 0x4E00 || ch.unicode() > 0x9FFF) {
            return false;
        }
    }

    return true;
}

void MainWindow::on_comboBox_2_activated(int index)
{
//    QVariant item=back_workerDataBase->Find("name !=' '");
     int workerPwd=index*6+2;
     int workerIdcard=index*6+4;
     int workerTel=index*6+1;
     int workerLv=index*6+5;
    QVariant itemPwd=back_workerDataBase->Find("name !=' '").value(workerPwd).toInt();
    QVariant itemIdcard=back_workerDataBase->Find("name !=' '").value(workerIdcard);
    QVariant itemTel=back_workerDataBase->Find("name !=' '").value(workerTel);
    QVariant itemLv=back_workerDataBase->Find("name !=' '").value(workerLv);

    //同步密码框信息
    ui->lineEdit_9->setText(itemPwd.toString());
    ui->lineEdit_10->setText(itemIdcard.toString());
    ui->lineEdit_11->setText(itemTel.toString());
if(itemLv.toInt()==30){
    ui->comboBoxLv->itemText(0);

}else {
ui->comboBoxLv->itemText(1);
}

}


/**
 * @brief MainWindow::on_outRoomBtn_dhy_clicked
 *
 * 退房逻辑
 * dhy
 */
void MainWindow::on_outRoomBtn_dhy_clicked()
{
    QString outRoomId = ui->outRoomId_dhy->text();

    QString find = QString("number = '%0' AND startDate != '%1' AND endDate != '%2'").arg(outRoomId).arg("1970-01-01").arg("1970-01-01");
    QVariantList roomInfo =  back_roomInfo->Find(find);
    bool hasRoom = roomInfo.value(0) == "行号：0";
    if (hasRoom) {
        qDebug() << "存在该房间";

        QString checkInTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        QString sql = QString("UPDATE roomInfo SET startDate = '%0', endDate = '%1', checkInDate = '%2' WHERE number = '%3' AND startDate != '%4' AND endDate != '%5'")
                .arg("1970-01-01").arg("1970-01-01").arg(checkInTime).arg(outRoomId).arg("1970-01-01").arg("1970-01-01");


        if (back_roomInfo->execute(sql)) {
            QMessageBox::information(nullptr, "Success", "退房成功", QMessageBox::Ok);
        }

    } else {
        QMessageBox::critical(this, "", "输入有误，请检查输入");
    }
}

/**
 * @brief MainWindow::on_addRoomBtn_clicked
 * 添加房间按钮
 * dhy
 */
void MainWindow::on_addRoomBtn_clicked()
{
    QString roomType = ui->addRoomComboBox_dhy->currentText();
    QString newRoomId = ui->addRoomId_dhy->text();

    // 1. 先查出 这个类别的房间 当前的房间号有哪些
    QString sql = QString("SELECT numbers FROM roomClass WHERE class = '%0';").arg(roomType);
    QSqlQuery query = back_roomClass->query_dhy(sql);
    QString currentRoomNumbers;
    if (query.next()) {
        currentRoomNumbers = query.value(0).toString();
    }


    QStringList list_id = currentRoomNumbers.split(",");
//    qDebug() << list_id;

    // 2. 判断要添加的房间号是否已经存在了
    if (list_id.contains(newRoomId)) {
        QMessageBox::critical(this, "", "房间已存在，换个房间号吧");
        return;
    }

    // 3. 将新的房间号添加到现有的房间号字符串中
    currentRoomNumbers += "," + newRoomId;


    // 4. 更新数据库
    sql = QString("UPDATE roomclass SET numbers = '%0' WHERE class = '%1'").arg(currentRoomNumbers).arg(roomType);
    if(back_roomClass->execute(sql)) {
        QMessageBox::information(this, "", "添加成功", QMessageBox::Ok);
    }

}


/**
 * @brief MainWindow::on_removeRoomBtn_clicked
 * 删除房间逻辑
 * dhy
 */
void MainWindow::on_removeRoomBtn_clicked()
{
    QString sql = "SELECT numbers FROM roomClass;";
    QSqlQuery query = back_roomClass->query_dhy(sql);

    QString removeRoomId = ui->removeRoomId_dhy->text();
    bool hasId = false;

    // 1. 判断要删除的房间号是否存在
    QString oldRoomIdList;
    while (query.next()) {
        QStringList list_id = query.value(0).toString().split(',');

        if (list_id.contains(removeRoomId)) {
            hasId = true;
            oldRoomIdList = query.value(0).toString(); // 为了之后更新数据
        }
    }

    if (!hasId) {
        QMessageBox::information(this, "", "不存在该房间，请检查输入");
        return;
    }


    // 2. 如果存在 判断该房间是否有人住
//    qDebug() << removeRoomId;
    QString find = QString("number = '%0' AND startDate != '%1' AND endDate != '%2'").arg(removeRoomId).arg("1970-01-01").arg("1970-01-01");
    QVariantList roomInfo =  back_roomInfo->Find(find);
    bool hasRoom = roomInfo.value(0) == "行号：0";

    if (hasRoom) {
        QMessageBox::critical(this, "", "该房间正在被使用");
        return;
    }

    // 3. 存在且 没有被使用 才能删除

//    qDebug() << oldRoomIdList;
    QStringList roomidStringList = oldRoomIdList.split(',');
    roomidStringList.removeOne(removeRoomId);


    QString updateIdList = roomidStringList.join(",");

//    qDebug() << updateIdList;
//    qDebug() << oldRoomIdList;
    sql = QString("UPDATE roomclass SET numbers = '%0' WHERE numbers = '%1'").arg(updateIdList).arg(oldRoomIdList);
    if (back_roomClass->execute(sql)) {
        QMessageBox::information(this, "", "删除成功", QMessageBox::Ok);
    }

}



/**
 * @brief MainWindow::on_changeRoomPriceBtn_clicked
 *
 * 修改房间价格
 * dhy
 */
void MainWindow::on_changeRoomPriceBtn_clicked()
{
    QString changeRoomType = ui->changeRoomPriceComboBox_dhy->currentText();

//    qDebug() << changeRoomType;

    QString sql;
    if (!isNumber(ui->newPrice->text())) {
        QMessageBox::information(this, "", "输入不合法，请检查输入");
        return;
    }

    int newPrice = ui->newPrice->text().toInt();
    sql = QString("UPDATE roomclass SET price = '%0' WHERE class = '%1'").arg(newPrice).arg(changeRoomType);
    if (back_roomClass->execute(sql)) {
        QMessageBox::information(this, "", "修改成功", QMessageBox::Ok);
    }

}


/**
 * @brief MainWindow::on_addRoomTypeBtn_clicked
 * 添加房间类型
 * dhy
 */
void MainWindow::on_addRoomTypeBtn_clicked()
{
    QString addRoomType = ui->addRoomType_dhy->text();
    QString addRoomTypeIdString = ui->addRoomTypeId->text();
    QString roomBrief = ui->addRoomTypeBrief_dhy->toPlainText();
    QString sql;
    QSqlQuery query;

    sql = "SELECT class FROM roomClass;";
    query = back_roomClass->query_dhy(sql);

    bool hasType = false;
    while (query.next()) {
        if (query.value(0).toString() == addRoomType) hasType = true;
    }

    if (hasType) {
        QMessageBox::critical(this, "", "该房间类型已经存在");
        return;
    }


    if (!isNumber(ui->addRoomTypePrice->text())) {
        QMessageBox::critical(this, "", "价格输入不合法, 请检查输入");
        return;
    }

    //校验图片是否合法
    QString image = ui->addRoomImage_rzx->text();
    QPixmap pixmap;
    if(!pixmap.load(image))  //图片合法
    {
        QMessageBox::critical(this, "Error", "图片选择错误, 请检查路径");
        return;
    }

    int roomTypePrice = ui->addRoomTypePrice->text().toInt();
    sql = QString("INSERT INTO roomclass VALUES ('%0', '%1', '%2', '%3', '%4')")
            .arg(addRoomType).arg(image).arg(roomBrief).arg(addRoomTypeIdString).arg(roomTypePrice);

    if (back_roomClass->execute(sql)) {
        QMessageBox::information(this, "", "添加成功", QMessageBox::Ok);
    }

    //清空数据
    ui->addRoomType_dhy->clear();
    ui->addRoomTypeId->clear();
    ui->addRoomTypePrice->clear();
    ui->addRoomTypeBrief_dhy->clear();
    ui->addRoomImage_rzx->clear();
    ui->label_33->setPixmap(QPixmap(":/new/images/images/imageMiss.png"));    //设置图片
    ui->label_33->setScaledContents(true); // 图片自适应label大小
    ui->label_33->show();
}

/**
 * @brief MainWindow::addRoomTypeComboBoxInit_dhy
 * 房间类型下拉框初始化：从数据库查出数据添加进去
 * dhy
 */
void MainWindow::addRoomTypeComboBoxInit_dhy()
{
    QString sql = "SELECT class FROM roomClass";
    QSqlQuery query = back_roomClass->query_dhy(sql);

    ui->addRoomComboBox_dhy->clear();
    ui->changeRoomPriceComboBox_dhy->clear();

    while (query.next()) {
        QString roomType = query.value(0).toString();
        ui->addRoomComboBox_dhy->addItem(roomType);
        ui->changeRoomPriceComboBox_dhy->addItem(roomType);
    }
}

void MainWindow::on_reset_rzx_clicked() //订房页面重置
{
    ui->inRoomTel->clear();  //手机号
    ui->inRoomIdentity->clear();    //身份证号
    ui->inRoomName->clear();  //姓名
    ui->inRoomId->clear();  //房间号
    ui->shifu_lineEdit_rzx->clear();    //实付价格
    //设置两个时间日期
    ui->startDateEdit->setDate(QDate::currentDate());
    ui->endDateEdit->setDate(QDate::currentDate().addDays(1));
    QMessageBox::information(this, "message", "重置成功");
    //清空图片
    QString image = ":/new/images/images/imageMiss.png";
    ui->label_su->setPixmap(QPixmap(image));    //设置图片
    ui->label_su->setScaledContents(true); // 图片自适应label大小
    ui->label_su->show();
}

void MainWindow::on_inRoomId_editingFinished()  //更新订房页面的图片、显示应付价格
{
    if(ui->inRoomId->text() != NULL){
        //获取数据库信息
        QVariantList find = back_roomClass->Find( QString("numbers like '%%1%'").arg(ui->inRoomId->text()));
        bool ifImage = find.value(0) == "行号：0";
        QString image;
        if(ifImage) //有路径
        //图片路径
            image = find.value(2).toString();
        else    //  没路径
            image = ":/new/images/images/imageMiss.png";
        ui->label_su->setPixmap(QPixmap(image));    //设置图片
        ui->label_su->setScaledContents(true); // 图片自适应label大小
        ui->label_su->show();

        //应付
        qlonglong yingfu_pricce = find.value(5).toLongLong();
        ui->yingfu_lineEdit_rzx->setText(QString::number(yingfu_pricce));
    }
    else{
        QString image = ":/new/images/images/imageMiss.png";
        ui->label_su->setPixmap(QPixmap(image));    //设置图片
        ui->label_su->setScaledContents(true); // 图片自适应label大小
        ui->label_su->show();

        //应付
        ui->yingfu_lineEdit_rzx->setText("查不到该房间价格。");
    }
}

void MainWindow::on_slectRoomImage_rzx_clicked()    //选择要添加的房间图片
{
    // 打开选择文件路径的对话框
    QString filePath = QFileDialog::getOpenFileName(nullptr, "选择文件", "", "All Files (*);;Text Files (*.txt)");

    //将路径显示出来
    ui->addRoomImage_rzx->setText(filePath);

    //将图片显示出来
    QString image = ":/new/images/images/imageMiss.png";
    QPixmap pixmap;
    if(pixmap.load(filePath))
        image = filePath;
    ui->label_33->setPixmap(QPixmap(image));    //设置图片
    ui->label_33->setScaledContents(true); // 图片自适应label大小
    ui->label_33->show();
}

void MainWindow::on_outRoomId_dhy_editingFinished()
{
    if(ui->outRoomId_dhy->text() != NULL){
        //获取数据库信息
        QVariantList find = back_roomClass->Find( QString("numbers like '%%1%'").arg(ui->outRoomId_dhy->text()));
        bool ifImage = find.value(0) == "行号：0";
        QString image;
        if(ifImage) //有路径
        //图片路径
            image = find.value(2).toString();
        else    //  没路径
            image = ":/new/images/images/imageMiss.png";
        ui->label_32->setPixmap(QPixmap(image));    //设置图片
        ui->label_32->setScaledContents(true); // 图片自适应label大小
        ui->label_32->show();
    }
    else{
        QString image = ":/new/images/images/imageMiss.png";
        ui->label_32->setPixmap(QPixmap(image));    //设置图片
        ui->label_32->setScaledContents(true); // 图片自适应label大小
        ui->label_32->show();
    }
}

void MainWindow::on_hotelBill_pushButton_rzx_clicked()  //酒店业务分析
{
    //获取数据库信息
    QVariantList find = back_hotelBill->Find("class != ' '");
    //处理数据库信息
    QString date[find.length() / 7];
    QString roomClass[find.length() / 7];
    QString number[find.length() / 7];
    QString collectMoney[find.length() / 7];
    QString collectedMoney[find.length() / 7];
    QString name[find.length() / 7];
    for (int i = 0;i < find.length() / 7;i++) {
        date[i] = find.value(i*7 + 1).toString();
        roomClass[i] = find.value(i*7 + 2).toString();
        number[i] = find.value(i*7 + 3).toString();
        collectMoney[i] = QString::number(find.value(i*7 + 4).toLongLong());
        collectedMoney[i] = QString::number(find.value(i*7 + 5).toLongLong());
        name[i] = find.value(i*7 + 6).toString();
    }
    //设置表格大小
    ui->hotelBill_tableWidget_rzx->setRowCount(find.length() / 7);
    ui->hotelBill_tableWidget_rzx->setColumnCount(6);
    //设置表格内容
    for (int i = 0; i < find.length() / 7;i++)
    {
        //设置各种类
        QLabel *date_l = new QLabel(date[i]);
        QLabel *roomClass_l = new QLabel(roomClass[i]);
        QLabel *number_l = new QLabel(number[i]);
        QLabel *collectMoney_l = new QLabel(collectMoney[i]);
        QLabel *collectedMoney_l = new QLabel(collectedMoney[i]);
        QLabel *name_l = new QLabel(name[i]);

        //设置内容
        ui->hotelBill_tableWidget_rzx->setCellWidget(i , 0,date_l);
        ui->hotelBill_tableWidget_rzx->setCellWidget(i , 1,roomClass_l);
        ui->hotelBill_tableWidget_rzx->setCellWidget(i , 2,number_l);
        ui->hotelBill_tableWidget_rzx->setCellWidget(i , 3,collectMoney_l);
        ui->hotelBill_tableWidget_rzx->setCellWidget(i , 4,collectedMoney_l);
        ui->hotelBill_tableWidget_rzx->setCellWidget(i , 5,name_l);
    }
    //设置宽度
    ui->hotelBill_tableWidget_rzx->setColumnWidth(0,190);
    ui->hotelBill_tableWidget_rzx->setColumnWidth(1,75);
    ui->hotelBill_tableWidget_rzx->setColumnWidth(2,75);
    ui->hotelBill_tableWidget_rzx->setColumnWidth(3,75);
    ui->hotelBill_tableWidget_rzx->setColumnWidth(4,75);
    ui->hotelBill_tableWidget_rzx->setColumnWidth(5,75);
    //设置列标题
    QStringList header;
    header.append("操作时间");
    header.append("房间类型");
    header.append("房间号");
    header.append("实际付款");
    header.append("应付款");
    header.append("操作人员");
    ui->hotelBill_tableWidget_rzx->setHorizontalHeaderLabels(header);

    //打开列表排序功能
    ui->hotelBill_tableWidget_rzx->setSortingEnabled(true);

    //切换到对应页面
    ui->stackedWidget->setCurrentIndex(5);
}

void MainWindow::on_hotelBill_tableWidget_rzx_cellActivated(int row, int column)
{
    if(row == 0 && column == 1){
        ui->hotelBill_tableWidget_rzx->sortByColumn(column, Qt::AscendingOrder);
    }
}



void MainWindow::on_pushButtonExit_clicked()
{
    this->close();
    login *l=new login;
    l->show();
}
